export class DBUtils {

  private collection: DB.CollectionReference

  private static instance: DBUtils

  private constructor(dbName: string) {
    this.collection = wx.cloud.database().collection(dbName)
  }

  public static getInstance(dbName: string): DBUtils {
    if (!this.instance) {
      this.instance = new DBUtils(dbName)
    }
    return this.instance
  }

  public getCollection() {
    return this.collection
  }

  public query(condition: DB.IQueryCondition): DB.Query {
    return this.collection.where(condition)
  }

}

export class User {

  public userName: string
  public passWord: string

  private constructor(un: string, pw: string) {
    this.userName = un
    this.passWord = pw
  }

  public static of(userName: string, passWord: string): User {
    return new User(userName, passWord)
  }

}

export class UserDao {

  private static db = DBUtils.getInstance('users')

  public static async login(user: User): Promise<DB.IQueryResult> {
    return await this.db.query({
      userName: user.userName,
      passWord: user.passWord
    }).get()
  }

}
